<template>
  <el-row>
    <el-col :span="20">
      <el-row gutter="24" style="padding: 20px">
        <template v-for="(item, index) in state.testItems" :key="index">
          <el-col :span="6">
            <TestCenterItem
              :status_cnt1="item.status_cnt1"
              :status_cnt2="item.status_cnt2"
              :status_cnt3="item.status_cnt3"
              :status_cnt4="item.status_cnt4"
              :testDuration="item.testDuration"
              :datatimestamp="item.数据时间戳"
              :voltage="item.整板电压"
              :temperature="item.温度"
              :status="item.状态"
              :boardid="index + 1"
              :testid="props.testid"
              :enableboardstatus="props.enableboardstatus"
              :type="props.type"
              :testInfo="props.testInfo"
              :datasourceindex="props.datasourceindex"
              :boardType="getBoardType(index + 1)"
              :deviceid="props.deviceid"
            /> </el-col
        ></template> </el-row
    ></el-col>
    <el-col :span="4"
      ><TestLog
        :type="props.type"
        :testid="props.testid"
        :boardid="state.boardid"
    /></el-col>
  </el-row>
</template>

<script setup>
import TestCenterItem from "./testcenteritem.vue";
import TestLog from "./testlog.vue";
import { ref, reactive, watch, onMounted, onUnmounted } from "vue";
import { icBoardTypelist } from "@/api/device/ICBoardType";
import { getDPComponentTimeStatus } from "@/api/device/dpcomponentTimeStatus";
import {
  getBoardTestInfoIC,
  getBoardTestInfoDP,
} from "@/api/device/boardTestInfo";
import { ignorableWatch } from "@vueuse/core";
const props = defineProps({
  type: {
    type: String,
    default: "ic",
  },
  testid: {
    type: String,
    default: "",
  },
  deviceid: {
    type: String,
    default: "",
  },
  enableboardstatus: {
    type: String,
    default: "",
  },
  testInfo: {
    type: Object,
    default: {},
  },
  datasourceindex: {
    type: String,
    default: "",
  },
});

const state = reactive({ boardid: "1", testItems: [], boardtypes: [] });
const getBoardType = (boardid) => {
  if (props.type == "ic") {
    let boardtype =
      state.boardtypes.filter((item) => item.boardNumber == `${boardid}`)[0] ||
      {};
    return boardtype.boardType;
  }
  return "";
};
// {
//   "状态": "正常",
//   "板卡ID": 0,
//   "整板电压": 0,
//   "整板电流": 0,
//   "测试状态": false,
//   "测试类型": 0,
//   "_芯片类型": 0,
//   "数据时间戳": "0001-01-01T00:00:00",
//   "实时片选信息": null,
//   "数据唯一标识ID": null,
//   "电源组开关状态": null,
//   "电源组损毁状态": null,
//   "自动测试周期数": 0
// }
const apiFn = ref(getBoardTestInfoIC);
const fetchInterval = ref(null);
// 封装获取数据的函数
const fetchData = () => {
  apiFn
    .value(props.testid, {
      datasourceindex: props.datasourceindex,
      type: props.type,
    })
    .then((res) => {
      state.testItems = res || [];
    });
};
// 设置定时任务
const setFetchInterval = () => {
  // 5 分钟执行一次，单位为毫秒
  fetchInterval.value = setInterval(fetchData, 5 * 60 * 1000);
};
// 清除定时任务
const clearFetchInterval = () => {
  if (fetchInterval.value) {
    clearInterval(fetchInterval.value);
  }
};
// 组件挂载时调用获取数据函数
onMounted(() => {
  // 根据初始 type 设置 apiFn
  if (props.type == "ic") {
    icBoardTypelist({ datasourceindex: props.datasourceindex }).then((res) => {
      state.boardtypes = res || {};
    });
  }
  apiFn.value = props.type === "ic" ? getBoardTestInfoIC : getBoardTestInfoDP;
  fetchData();
  clearFetchInterval();
  setFetchInterval();
});

// 组件卸载时清除定时任务
onUnmounted(() => {
  clearFetchInterval();
});
// watch(
//   [() => props.type, () => props.testid],
//   ([newType, newTestId], [oldType, oldTestId]) => {
//     // 当 type 变化时，更新 apiFn
//     if (newType !== oldType) {
//       apiFn.value = newType === "ic" ? getBoardTestInfoIC : getBoardTestInfoDP;
//     }
//     // 重新调用 API 获取数据
//     fetchData();
//     // 重置定时任务
//     clearFetchInterval();
//     setFetchInterval();
//   },
//   { deep: true }
// );
</script>
